home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapter6 / cracctbl.c < prev    next >
C/C++ Source or Header  |  1996-01-15  |  8KB  |  269 lines

  1.  
  2. #include <windows.h>  
  3. #include "CrAccTbl.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. HACCEL hAccel = NULL;
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    // Register the main application window class.
  33.    //............................................
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    // Create the main application window.
  56.    //....................................
  57.    hWnd = CreateWindow( lpszAppName, 
  58.                         lpszTitle,    
  59.                         WS_OVERLAPPEDWINDOW, 
  60.                         CW_USEDEFAULT, 0, 
  61.                         CW_USEDEFAULT, 0,  
  62.                         NULL,              
  63.                         NULL,              
  64.                         hInstance,         
  65.                         NULL               
  66.                       );
  67.  
  68.    if ( !hWnd ) 
  69.       return( FALSE );
  70.  
  71.    ShowWindow( hWnd, nCmdShow ); 
  72.    UpdateWindow( hWnd );         
  73.  
  74.    while( GetMessage( &msg, NULL, 0, 0) )   
  75.    {
  76.       if ( !hAccel || !TranslateAccelerator( hWnd, hAccel, &msg ) )
  77.       {
  78.          TranslateMessage( &msg ); 
  79.          DispatchMessage( &msg );  
  80.       }
  81.    }
  82.  
  83.    return( msg.wParam ); 
  84. }
  85.  
  86.  
  87. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  88. {
  89.    WNDCLASSEX wcex;
  90.  
  91.    wcex.style         = lpwc->style;
  92.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  93.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  94.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  95.    wcex.hInstance     = lpwc->hInstance;
  96.    wcex.hIcon         = lpwc->hIcon;
  97.    wcex.hCursor       = lpwc->hCursor;
  98.    wcex.hbrBackground = lpwc->hbrBackground;
  99.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  100.    wcex.lpszClassName = lpwc->lpszClassName;
  101.  
  102.    // Added elements for Windows 95.
  103.    //...............................
  104.    wcex.cbSize = sizeof(WNDCLASSEX);
  105.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  106.                             IMAGE_ICON, 16, 16,
  107.                             LR_DEFAULTCOLOR );
  108.             
  109.    return RegisterClassEx( &wcex );
  110. }
  111.  
  112.  
  113. #define IDM_NEWBASE 300
  114.  
  115. VOID AddNewTestItem( HWND hWnd )
  116. {
  117. static int nNum = 0;
  118.  
  119.    char   szMenuItem[20];
  120.  
  121.    HMENU  hMenu      = GetMenu( hWnd );
  122.    ACCEL* pAccelData = NULL;
  123.    ACCEL* pCurAccel  = NULL;
  124.    HANDLE hAccelData = NULL;
  125.    int    nNumAccel  = 1;
  126.  
  127.    // Maximum of 4 new items allowed.
  128.    //................................
  129.    if ( nNum == 4 )
  130.       return;
  131.  
  132.    // If accelerator table exists, get the number of items.
  133.    //......................................................   
  134.    if ( hAccel )
  135.    {
  136.       nNumAccel = CopyAcceleratorTable( hAccel, NULL, 0 ) + 1;
  137.    }
  138.  
  139.    // Allocate an array of ACCEL structures.
  140.    //.......................................
  141.    hAccelData = GlobalAlloc( GHND, sizeof(ACCEL) * nNumAccel );
  142.    if ( hAccelData )
  143.       pAccelData = (ACCEL*)GlobalLock( hAccelData );
  144.  
  145.    // If an accelerator table exists, copy the items into
  146.    // the newly allocated array.
  147.    //....................................................
  148.    if ( hAccel && pAccelData )
  149.    {
  150.       CopyAcceleratorTable( hAccel, pAccelData, nNumAccel-1 );
  151.  
  152.       DestroyAcceleratorTable( hAccel );
  153.       hAccel = NULL;
  154.    }
  155.  
  156.    // Add the new menu option and accelerator key
  157.    //............................................
  158.    if ( pAccelData )
  159.    {
  160.       // Get a pointer to the new accelerator key in
  161.       // the array.
  162.       //............................................
  163.       pCurAccel = (ACCEL*)(pAccelData+nNumAccel-1);
  164.  
  165.       // Create a new menu option on the menu.
  166.       //......................................
  167.       nNum++;
  168.       wsprintf( szMenuItem, "New Item&%d", nNum );
  169.       AppendMenu( hMenu, MFT_STRING, IDM_NEWBASE+nNum, szMenuItem );
  170.       DrawMenuBar( hWnd );
  171.  
  172.       // Set up a new accelerator of F1,F2,F3,or F4 for the
  173.       // the new menu option.
  174.       //...................................................
  175.       pCurAccel->fVirt = FNOINVERT | FVIRTKEY;
  176.       pCurAccel->cmd   = IDM_NEWBASE+nNum;
  177.       pCurAccel->key   = ( nNum == 1 ? VK_F1 :
  178.                            nNum == 2 ? VK_F2 :
  179.                            nNum == 3 ? VK_F3 :
  180.                            /*default*/ VK_F4 );
  181.  
  182.       // Create the new accelerator table.
  183.       //..................................
  184.       hAccel = CreateAcceleratorTable( pAccelData, nNumAccel );
  185.  
  186.       GlobalUnlock( hAccelData );
  187.    }
  188.  
  189.    if ( hAccelData )
  190.       GlobalFree( hAccelData );
  191. }
  192.  
  193.  
  194. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  195. {
  196.    switch( uMsg )
  197.    {
  198.       case WM_COMMAND :
  199.               switch( LOWORD( wParam ) )
  200.               {
  201.                  case IDM_TEST :
  202.                          AddNewTestItem( hWnd );
  203.                          break;
  204.  
  205.                  case IDM_NEWBASE+1 :
  206.                          MessageBox( hWnd, "New Item 1 Selected", "Item Selected", 
  207.                                      MB_OK | MB_ICONINFORMATION );
  208.                          break;
  209.  
  210.                  case IDM_NEWBASE+2 :
  211.                          MessageBox( hWnd, "New Item 2 Selected", "Item Selected", 
  212.                                      MB_OK | MB_ICONINFORMATION );
  213.                          break;
  214.  
  215.                  case IDM_NEWBASE+3 :
  216.                          MessageBox( hWnd, "New Item 3 Selected", "Item Selected", 
  217.                                      MB_OK | MB_ICONINFORMATION );
  218.                          break;
  219.  
  220.                  case IDM_NEWBASE+4 :
  221.                          MessageBox( hWnd, "New Item 4 Selected", "Item Selected", 
  222.                                      MB_OK | MB_ICONINFORMATION );
  223.                          break;
  224.  
  225.                  case IDM_ABOUT :
  226.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  227.                         break;
  228.  
  229.                  case IDM_EXIT :
  230.                         DestroyWindow( hWnd );
  231.                         break;
  232.               }
  233.               break;
  234.       
  235.       case WM_DESTROY :
  236.               PostQuitMessage(0);
  237.               break;
  238.  
  239.       default :
  240.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  241.    }
  242.  
  243.    return( 0L );
  244. }
  245.  
  246.  
  247. LRESULT CALLBACK About( HWND hDlg,           
  248.                         UINT message,        
  249.                         WPARAM wParam,       
  250.                         LPARAM lParam)
  251. {
  252.    switch (message) 
  253.    {
  254.        case WM_INITDIALOG: 
  255.                return (TRUE);
  256.  
  257.        case WM_COMMAND:                              
  258.                if (   LOWORD(wParam) == IDOK         
  259.                    || LOWORD(wParam) == IDCANCEL)    
  260.                {
  261.                        EndDialog(hDlg, TRUE);        
  262.                        return (TRUE);
  263.                }
  264.                break;
  265.    }
  266.  
  267.    return (FALSE); 
  268. }
  269.